/**
 * Copyright (c) 2022 aoshiguchen
 * <p>
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * <p>
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * <p>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.sl.proxy.server.controller;

import com.sl.proxy.server.base.page.PageInfo;
import com.sl.proxy.server.base.page.PageQuery;
import com.sl.proxy.server.controller.req.proxy.*;
import com.sl.proxy.server.controller.res.proxy.*;
import com.sl.proxy.server.service.LicenseService;
import com.sl.proxy.server.util.ParamCheckUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: aoshiguchen
 * @date: 2022/8/6
 */
@RequestMapping("/license")
@RestController
@RequiredArgsConstructor
public class LicenseController {
    private final LicenseService licenseService;

    @GetMapping("/page")
    public PageInfo<LicenseListRes> page(PageQuery pageQuery, LicenseListReq req) {
        ParamCheckUtil.checkNotNull(pageQuery, "pageQuery");
        return licenseService.page(pageQuery, req);
    }

    @PostMapping("/list")
    public List<LicenseListRes> list(@RequestBody LicenseListReq req) {
        return licenseService.list(req);
    }

    @PostMapping("/auth-list")
    public List<LicenseListRes> queryCurUserLicense(@RequestBody LicenseListReq req) {
        return licenseService.queryCurUserLicense(req);
    }

    @PostMapping("/create")
    public LicenseCreateRes create(@RequestBody LicenseCreateReq req) {
        ParamCheckUtil.checkNotNull(req, "req");
        ParamCheckUtil.checkNotEmpty(req.getName(), "name");
        ParamCheckUtil.checkNotNull(req.getUserId(), "userId");
        ParamCheckUtil.checkBytesDesc(req.getUpLimitRate(), "upLimitRate");
        ParamCheckUtil.checkBytesDesc(req.getDownLimitRate(), "downLimitRate");

        return licenseService.create(req);
    }

    @PostMapping("/update")
    public LicenseUpdateRes update(@RequestBody LicenseUpdateReq req) {
        ParamCheckUtil.checkNotNull(req, "req");
        ParamCheckUtil.checkNotNull(req.getId(), "id");
        ParamCheckUtil.checkNotEmpty(req.getName(), "name");
        ParamCheckUtil.checkBytesDesc(req.getUpLimitRate(), "upLimitRate");
        ParamCheckUtil.checkBytesDesc(req.getDownLimitRate(), "downLimitRate");

        return licenseService.update(req);
    }

    @PostMapping("/detail")
    public LicenseDetailRes detail(@RequestBody LicenseDetailReq req) {
        ParamCheckUtil.checkNotNull(req, "req");
        ParamCheckUtil.checkNotNull(req.getId(), "id");

        return licenseService.detail(req.getId());
    }

    @PostMapping("/update/enable-status")
    public LicenseUpdateEnableStatusRes updateEnableStatus(@RequestBody LicenseUpdateEnableStatusReq req) {
        ParamCheckUtil.checkNotNull(req, "req");
        ParamCheckUtil.checkNotNull(req.getId(), "id");
        ParamCheckUtil.checkNotNull(req.getEnable(), "enable");

        return licenseService.updateEnableStatus(req);
    }

    @PostMapping("/delete")
    public void delete(@RequestBody LicenseDeleteReq req) {
        ParamCheckUtil.checkNotNull(req, "req");
        ParamCheckUtil.checkNotNull(req.getId(), "id");

        licenseService.delete(req.getId());
    }

    @PostMapping("/reset")
    public void reset(@RequestBody LicenseResetReq req) {
        ParamCheckUtil.checkNotNull(req, "req");
        ParamCheckUtil.checkNotNull(req.getId(), "id");

        licenseService.reset(req.getId());
    }

}
